package com.geovis.emergency.spd.biz.pc.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.geovis.emergency.spd.biz.pc.mapper.PcDrillPlanTaskStatisticsMapper;
import com.geovis.emergency.spd.biz.pc.service.IPcDrillPlanTaskStatisticsService;
import com.geovis.emergency.spd.entity.mobile.pojo.vo.PcDrillPlanTaskStatisticsVO;
import com.geovis.emergency.spd.entity.pc.entity.PcDrillPlanTaskStatistics;
import org.springframework.stereotype.Service;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 应急预案指挥模块-演练工作方案任务_情况统计 服务实现类
 * </p>
 *
 * @author lmolong
 * @since 2022-06-21
*/

@Service
@RequiredArgsConstructor
@Slf4j
public class PcDrillPlanTaskStatisticsServiceImpl extends ServiceImpl<PcDrillPlanTaskStatisticsMapper, PcDrillPlanTaskStatistics> implements IPcDrillPlanTaskStatisticsService {

    @Override
    public List<PcDrillPlanTaskStatisticsVO> getByTaskId(String taskId) {
        LambdaQueryWrapper<PcDrillPlanTaskStatistics> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(PcDrillPlanTaskStatistics::getPcDrillPlanTaskId,taskId);
        List<PcDrillPlanTaskStatistics> list = list(wrapper);
        return list.stream().map(e->{
            PcDrillPlanTaskStatisticsVO vo = new PcDrillPlanTaskStatisticsVO();
            vo.setStatisticsType(e.getStatisticsType());
            vo.setStatisticsCount(e.getStatisticsCount());
            return vo;
        }).collect(Collectors.toList());
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void save(String taskId,List<PcDrillPlanTaskStatisticsVO> vos){
        LambdaQueryWrapper<PcDrillPlanTaskStatistics> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(PcDrillPlanTaskStatistics::getPcDrillPlanTaskId,taskId);
        baseMapper.delete(wrapper);
        if (CollUtil.isNotEmpty(vos)){
            List<PcDrillPlanTaskStatistics> data = vos.stream().map(e -> {
                PcDrillPlanTaskStatistics taskStatistics = new PcDrillPlanTaskStatistics();
                taskStatistics.setPcDrillPlanTaskId(taskId);
                taskStatistics.setStatisticsType(e.getStatisticsType());
                taskStatistics.setStatisticsCount(e.getStatisticsCount());
                return taskStatistics;
            }).collect(Collectors.toList());
            saveBatch(data);
        }

    }
}
